package com.wxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxy.entity.Saleacdata;
import com.wxy.entity.Saledata;
import com.wxy.entity.Salemonthdata;
import com.wxy.mapper.SaleacdataMapper;
import com.wxy.mapper.SaledataMapper;
import com.wxy.mapper.SalemonthdataMapper;
import com.wxy.selectUser.SelectUser;
import com.wxy.vo.EchartAcDate;
import com.wxy.vo.EchartDayData;
import com.wxy.vo.EchartMonthData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2022-01-12
 */
@RestController
@RequestMapping("/saleAcData")
public class SaleacdataController {

    @Autowired
    SaleacdataMapper saleAcDataMapper;
    @Autowired
    SaledataMapper saleDataMapper;
    @Autowired
    SalemonthdataMapper saleMonthDataMapper;

//  datastastic chart 1-3数据
    @GetMapping("/acData")
    public EchartAcDate acData(
            HttpServletRequest request
    ){
        EchartAcDate echartAcDate = new EchartAcDate();
        List<Float> profit = new ArrayList<>();
        List<Float> output = new ArrayList<>();
        List<Float> income = new ArrayList<>();
        //找到要插入的表
        QueryWrapper qw = SelectUser.saleAcDataSelectUser(request.getHeader("userId"));
        qw.last("limit 7");
        qw.orderByAsc("acIncome");
        List<Saleacdata> saleacdata =  saleAcDataMapper.selectList(qw);
        for (Saleacdata saleacdatum : saleacdata) {
            profit.add(saleacdatum.getAcProfit());
        }
        for (Saleacdata saleacdatum : saleacdata) {
            output.add(saleacdatum.getAcCost());
        }
        for (Saleacdata saleacdatum : saleacdata) {
            income.add(saleacdatum.getAcIncome());
        }
        echartAcDate.setProfit(profit);
        echartAcDate.setIncome(income);
        echartAcDate.setOutput(output);
        return echartAcDate;
    }


    //近十五日销售数据
    @GetMapping("/getDayDate")
    public EchartDayData getDayDate(
            HttpServletRequest request
    ){
        QueryWrapper qw = SelectUser.saleDataSelectUser(request.getHeader("userId"));
        qw.orderByDesc("id");
        qw.last("limit 15");
        List<Saledata> saledata = saleDataMapper.selectList(qw);
        System.out.println(saledata);
        List<Integer> profit = new ArrayList<>();
        List<Integer> income = new ArrayList<>();
        List<Integer> output = new ArrayList<>();
        List<Integer> growth = new ArrayList<>();
        for (Saledata saledatum : saledata) {
            profit.add(saledatum.getProjectProfit());
        }
        for (Saledata saledatum : saledata) {
            income.add(saledatum.getProjectIncome());
        }
        for (Saledata saledatum : saledata) {
            output.add(saledatum.getProjectCost());
        }
        for (Saledata saledatum : saledata) {
            growth.add(Integer.valueOf(saledatum.getGrowth()));
        }
        Collections.reverse(profit);
        Collections.reverse(income);
        Collections.reverse(output);
        Collections.reverse(growth);
        EchartDayData echartDayData = new EchartDayData();
        echartDayData.setGrowth(growth);
        echartDayData.setProfit(profit);
        echartDayData.setIncome(income);
        echartDayData.setOutput(output);
        return echartDayData;
    }


    //月度销售情况折线
    @GetMapping("/getMonthData")
    public EchartMonthData getMonthData(
            HttpServletRequest request
    ){
        QueryWrapper qw =  SelectUser.saleMonthDataSelectUser(request.getHeader("userID"));
        qw.orderByAsc("time");
        qw.last("limit 12");
        List<Salemonthdata> salemonthdata = saleMonthDataMapper.selectList(qw);
        List<Integer> monthProfit = new ArrayList<>();
        List<Integer> monthOutput = new ArrayList<>();
        List<Integer> monthIncome = new ArrayList<>();
        EchartMonthData echartMonthData = new EchartMonthData();
        for (Salemonthdata salemonthdatum : salemonthdata) {
            monthProfit.add(salemonthdatum.getProjectProfit());
        }
        for (Salemonthdata salemonthdatum : salemonthdata) {
            monthOutput.add(salemonthdatum.getProjectCost());
        }
        for (Salemonthdata salemonthdatum : salemonthdata) {
            monthIncome.add(salemonthdatum.getProjectIncome());
        }
        echartMonthData.setMonthIncome(monthIncome);
        echartMonthData.setMonthOutput(monthOutput);
        echartMonthData.setMonthProfit(monthProfit);
        return echartMonthData;
    }
}

